<?php

class wxapi_ctl_wxmember extends wxapi_frontpage
{
    public function __construct(&$app)
    {
        parent::__construct($app);
        $this->userPassport = kernel::single('b2c_user_passport');
    }

    public function jscode2session()
    {  
        $appId = constant('WX_APPID');
        $secret = constant('WX_TOKEN');
        
        $code = trim($_REQUEST['jsCode']);
        $url = sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", $appId, $secret, $code);
        //{"session_key":"Wa7QpEHzmZ5PyuwZ43nQkw==","openid":"oeJKo5Z_ABFI7E_b5vYIR2THA-1o"}
        //$res = kernel::single('base_httpclient')->get($url);
        $res = file_get_contents($url);
//      url := fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", w.Appid, w.Secret, w.Code)
        $user = json_decode($res, true);//['session_key'=>'', 'openid'=>''];
  
        //$user = json_decode('{"session_key":"Wa7QpEHzmZ5PyuwZ43nQkw==","openid":"oeJKo5Z_ABFI7E_b5vYIR2THA-1o"}', 1);//['session_key'=>'', 'openid'=>''];
        $session_key = $user['session_key'];
        $openid = $user['openid'];
        if($openid){            
            //$trustLoginInfo = app::get('trustlogin')->model('trustinfo')->getRow('member_id,openid', ['openid'=>$openid]);
            $mdl_pam_members = app::get('pam')->model('members');
            $rs = $mdl_pam_members->dump(array('login_account' => $openid));
            if(!$rs){
                $userInitData = [
                    'pam_account' => [
                        'login_type'=>'local',
                        'login_account' => $openid,
                        'login_password' => md5($openid.'shopex123'),
                        'password_account' => $openid,
                        'disabled' => 'false',
                        'createtime' => time(),
                    ],
                    'b2c_members' => [
                        'member_lv' => [
                            'member_group_id'=> '1',
                        ],
                        'currency' => 'CNY',
                        'reg_ip' => $_SERVER["REMOTE_ADDR"],
                        'regtime' => time(),
                    ],
                ];
                $member_id = $this->userPassport->save_members($userInitData, $msg);

                /*
                $trustLogininfo = [
                    'member_id'=>$member_id,
                    'openid' => $openid,
                ];
                app::get('trustlogin')->model('trustinfo')->save($trustLogininfo);
                */
            }
            
            $data = array(
                'result' => true,
                'data' => array(
                    'session_key' => $session_key,
                    'expires_in'=>720000,
                    'openid' => $openid,
                )
            );
            echo(json_encode($data));            
        }else{
            $this->splash('failed',null,'获取openid失败:'.$res,'','',true);            
        }
        
        exit;
    }
}
